package com.ruoyi.web.controller.nongjt;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.nongjt.domain.NjtJobPlatform;
import com.ruoyi.nongjt.service.NjtJobPlatformService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nongjt/jobPlatform")
public class NjtJobPlatformController extends BaseController {
    @Autowired
    private NjtJobPlatformService njtJobPlatformService;

    @RequestMapping("/list")
    public TableDataInfo list(NjtJobPlatform njtJobPlatform){
        startPage();
        List<NjtJobPlatform> list = njtJobPlatformService.getNjtJobPlatformList(njtJobPlatform);
        return getDataTable(list);
    }

    @PutMapping
    public AjaxResult update(@RequestBody NjtJobPlatform njtJobPlatform){
        int rows = njtJobPlatformService.updateNjtJobPlatform(njtJobPlatform);
        return toAjax(rows);
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        int rows = njtJobPlatformService.deleteById(id);
        return toAjax(rows);
    }
    @DeleteMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) { // @RequestBody 接收前端传的 ID 数组
        if (ids == null || ids.isEmpty()) {
            return AjaxResult.error("请选择要删除的记录");
        }
        int count = njtJobPlatformService.batchDeleteJobPlatform(ids);

        return count > 0 ? AjaxResult.success("成功删除 " + count + " 条记录") : AjaxResult.error("删除失败");
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, NjtJobPlatform njtJobPlatform) {
        List<NjtJobPlatform> list = njtJobPlatformService.getNjtJobPlatformList(njtJobPlatform);
        ExcelUtil<NjtJobPlatform> util = new ExcelUtil<NjtJobPlatform>(NjtJobPlatform.class);
        util.exportExcel(response, list, "检查平台数据");
    }
    @PutMapping("/add")
    public AjaxResult add(@RequestBody NjtJobPlatform njtJobPlatform){
        int rows = njtJobPlatformService.addNjtJobPlatform(njtJobPlatform);
        return toAjax(rows);
    }
}